//
//  HomeViewController.swift
//  XMYouPin
//
//  Created by 王宗成 on 2019/7/2.
//  Copyright © 2019 王宗成. All rights reserved.
//

import UIKit
import SnapKit
import DNSPageView
import HandyJSON
class HomeViewController: WZC_ViewController {

    lazy var topNavView:WRCustomNavigationBar = {
        
        return WRCustomNavigationBar.CustomNavigationBar()
    } ()
    lazy var searchView:WZC_SearchView = {
        
       return WZC_SearchView.init(frame: CGRect.init(x: 0, y: 0, width: kScreenW-80, height: 30))
    }()
    
    lazy var leftNavImgView:UIImageView = {
        
        return UIImageView.init(image: IMAGE("mjyp_navi_logo"))
    }()
    
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
        
        layoutSubViewUI()
        
        setupPageStyle()
     
    }
    
    func setupPageStyle() {
        // 创建DNSPageStyle，设置样式
        let style = DNSPageStyle()
        style.isTitleViewScrollEnabled = true
        style.isTitleScaleEnabled = true
        style.isShowBottomLine = true
        style.titleSelectedColor = UIColor.hexColor(hexadecimal: kThemeColor)
        style.titleViewHeight = 35
        style.titleColor = UIColor.gray
        style.bottomLineColor = UIColor.hexColor(hexadecimal: kThemeColor)
        style.bottomLineHeight = 2
        

        let datas = loadData()
        
        var titles:Array<String> = []
        
        var viewControllers:[HomeSubViewController] = []
        
        for model in datas {
            
            titles.append(model.name!)
            
            viewControllers.append(HomeSubViewController())
            
        }
        
        for vc in viewControllers{
            
            self.addChild(vc)
            
        }
        
        let pageView = DNSPageView(frame: CGRect(x: 0, y: kNavigationBarHeight, width: kScreenW, height: kScreenH - kNavigationBarHeight - 40), style: style, titles: titles, childViewControllers: viewControllers)
        
        pageView.contentView.backgroundColor = UIColor.red
        
        view.addSubview(pageView)
        
    }
    
    func loadData() -> Array<HomeClassModel> {
        
        let path = Bundle.main.path(forResource: "HomeClass", ofType: "json")
        // 2. 获取json文件里面的内容,NSData格式
        let jsonData=NSData(contentsOfFile: path!)
        // 3. 解析json内容
        
        do {
            let json = try JSONSerialization.jsonObject(with: jsonData! as Data, options: .allowFragments)
            
            if let result = json as? Dictionary<String, Any> {
            
                return [HomeClassModel].deserialize(from: result["data"] as? [Any])! as! Array<HomeClassModel>
                
            }
            else
            {
                return []
            }
        } catch  {
            
            print("解析失败")
            return []
        }
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        self.navigationController?.navigationBar.isHidden = true
    }
    
}

extension HomeViewController
{
    func layoutSubViewUI() -> Void {
        
        self.view.addSubview(self.topNavView)
        
        self.topNavView.wr_setBottomLineHidden(hidden: true)
        
        self.topNavView.wr_setRightButton(image: IMAGE("mjyp_navi_main_page_msg_normal"))
        
        self.topNavView.addSubview(self.searchView)
        
        self.topNavView.addSubview(self.leftNavImgView)
        
        self.leftNavImgView.snp.makeConstraints { (make) in
            
            make.left.equalTo(8)
            make.centerY.equalTo(self.searchView.snp_centerY)
        }
        
        self.searchView.snp.makeConstraints { (make) in
            
            make.left.equalTo(self.leftNavImgView.snp_right).offset(8)
            make.bottom.equalTo(-5)
            make.right.equalTo(self.topNavView.rightButton.snp_left)
            make.height.equalTo(32)
        }
    }
}
